package com.liyunc.demo.bundle.user.org.utils.converter;

import com.liyunc.demo.bundle.user.org.constant.UserPersonConstant;
import com.liyunc.demo.bundle.user.org.model.dto.input.PersonRegisterRequestDto;
import com.liyunc.demo.bundle.user.org.model.dto.output.UserPersonalAccountDto;
import com.liyunc.demo.bundle.user.org.model.dto.output.PersonRegisterResponseDto;
import com.liyunc.demo.bundle.user.org.model.entity.UserPerson;
import com.liyunc.demo.bundle.user.org.model.entity.UserPersonalAccount;
import org.apache.commons.lang3.RandomStringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserOrganizationConverter.java
 * @brief UserOrganizationConverter
 * @details UserOrganizationConverter
 * @date 2022-04-01
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-04-01                  liyuncong          Created
 */
@Mapper
public interface UserPersonConverter {

    @Mappings(value = {
        @Mapping(target = "userPersonalAccountId", source = "id"),
        @Mapping(target = "userPersonDto.userPersonId", source = "person.id")
    })
    UserPersonalAccountDto fromEntity(UserPersonalAccount account);

    default UserPersonalAccount fromRegisterDto(
        PersonRegisterRequestDto requestDto,
        PasswordEncoder passwordEncoder
    ) {
        final String salt = RandomStringUtils.randomAlphabetic(UserPersonConstant.SALT_LENGTH);
        final String password = passwordEncoder.encode(requestDto.getLoginPassword());
        UserPersonalAccount account = new UserPersonalAccount();
        account.setLoginUsername(requestDto.getLoginUsername());
        account.setLoginPassword(password);
        account.setAlias(requestDto.getLoginUsername());
        account.setLoginPasswordSalt(salt);
        account.setLoginType(UserPersonConstant.LOGIN_TYPE_DEFAULT);
        return account;
    }

    default PersonRegisterResponseDto toRegisterResult(
        UserPerson userinfo, UserPersonalAccount account
    ) {
        PersonRegisterResponseDto responseDto = new PersonRegisterResponseDto();
        responseDto.setUserPersonId(userinfo.getId());
        responseDto.setLoginUsername(account.getLoginUsername());
        responseDto.setUserPersonalAccountId(account.getId());
        responseDto.setCreatedAt(account.getCreatedAt());
        responseDto.setCreatedBy(account.getCreatedBy());
        responseDto.setUpdatedAt(account.getUpdatedAt());
        responseDto.setCreatedBy(account.getCreatedBy());
        return responseDto;
    }

    default UserPerson fromRegisterDto(PersonRegisterRequestDto requestDto) {
        UserPerson userinfo = new UserPerson();
        userinfo.setUsername(requestDto.getUsername());
        userinfo.setAuthStatus(UserPersonConstant.AUTH_STATUS_DEFAULT);
        return userinfo;
    }
}
